package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.UserGroupEntity;

/**
 * 用户组业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class UserGroupCheck extends BaseCheck {

	/**
	 * 用户组新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 用户组编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 用户组删除时检查
	 * 
	 * @param group
	 *            用户组对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(UserGroupEntity group) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (group == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户组"));
			return chkRS;
		}

		if (group.getUsers().size() > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该用户组已关联用户，无法删除");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户组名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户组名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "用户组名称", 32));
			return chkRS;
		}

		// 描述非空检查
		String description = (String) paramMap.get("description");
		if (StringUtils.trim(description).length() > 255) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 255));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户组名称重复性检查
		String name = (String) paramMap.get("name");
		if (userGroupDao.countByName(name) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "用户组名称"));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户组编码非空检查
		String id = (String) paramMap.get("id");
		if (StringUtils.isBlank(id)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户组编码"));
			return chkRS;
		}

		// 用户组名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户组名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "用户组名称", 32));
			return chkRS;
		}

		// 描述长度检查
		String description = (String) paramMap.get("description");
		if (StringUtils.trim(description).length() > 255) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 255));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户组存在性检查
		String id = (String) paramMap.get("id");
		UserGroupEntity group = userGroupDao.get(id);
		if (group == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户组"));
			return chkRS;
		}

		// 用户组名称重复性检查
		String name = (String) paramMap.get("name");
		if (!group.getName().equals(name)) {
			if (userRoleDao.countByName(name) > 0) {
				chkRS.setStatus(HttpStatus.SC_CONFLICT);
				chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "用户组名称"));
				return chkRS;
			}
		}

		return chkRS;
	}

}
